package com.kcht.np.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kcht.np.entity.Relation;
import com.kcht.np.entity.SysUser;
import com.kcht.np.vo.page.QueryParameter;
import com.kcht.np.vo.user.UserCanModifyInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
  * <p>
  * 用户 Mapper 接口
  * </p>
  *
  * @Package:        com.kcht.np.mp.mapper
  * @Description:    用户 Mapper 接口
  * @Author:         liuqi
  * @CreateDate:     2020/3/31 21:22
 */
public interface SysUserMapper extends BaseMapper<SysUser> {

    /**
     * 查询出用户名和昵称可以匹配关键字的所有结果
     * 并将结果分页
     */
    List<SysUser> pageByKeywords(QueryParameter parameter);

    /**
     * 根据用户名和昵称模糊查询的结果数量
     * */
    int countByKeywords(String keywords);

    /**
     * 授予多个角色，注意不要重复授予
     */
    int grantRoles(List<Relation> record);

    /**
     * 解除授予标识为{uid}用户的角色
     */
    int deleteHaveRoles(Long uid);

    /**
     * 用户上线
     * */
    @Update("update tb_sys_user u set u.`status`=2 where u.username=#{username}")
    void userOnLine(String username);

    /**
     * 用户离线
     * */
    @Update("update tb_sys_user u set u.`status`=1 where u.username=#{username}")
    void userOffLine(String username);

    /**
     * 判断用户是否在线
     * */
    @Select("select `status` from tb_sys_user where username=#{username}")
    boolean isOnLine(String username);

    @Select("select * from tb_sys_user where username=#{username}")
    SysUser queryUserByUsername(String username);

    void userUpdate(@Param("username") String username, @Param("modify") UserCanModifyInfo modify);

    @Select("select username from tb_sys_user where uid=#{uid}")
    String queryUsernameById(Long uid);

    @Update("update tb_sys_user set dept_id=#{deptId} where uid=#{uid}")
    void updateUserDept(Long uid,Long deptId);
}
